var api_url1 = 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items';
var api_url2 = 'https://m.douban.com/rexxar/api/v2/subject_collection/tv_hot/items';
var api_url3 = 'https://m.douban.com/rexxar/api/v2/subject_collection/tv_variety_show/items';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modelData1: [],
    modelData2: [],
    modelData3: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getMovieData(10)
    that.getTvData(10)
    that.getTvsData(10)
  },

  /**
   * 获取电视剧资讯
   */
  getTvData: function(count) {
    var that = this;
    wx.request({
      url: api_url3,
      data: {
        count: count
      },
      success: (res) => {
        var subjects = res.data.subject_collection_items;

        subjects.forEach(function(item, index) {
          item.rate = null;
          try {
            item.rate = item.rating.value;
          } catch (error) {
            item.rate = 0;
          }
          var rate = item.rate;
          var heightLight = parseInt(parseInt(rate) / 2);
          var half = parseInt(rate) % 2;
          var gray = 5 - heightLight - half;
          var heightLights = [];
          var halfs = [];
          var grays = [];

          for (var i = 0; i < heightLight; i++) {
            heightLights.push(i);
          }

          for (var i = 0; i < half; i++) {
            halfs.push(i);
          }

          for (var i = 0; i < gray; i++) {
            grays.push(i);
          }

          item.heightLights = heightLights;
          item.halfs = halfs;
          item.grays = grays;
        })
        that.setData({
          modelData2: subjects
        })
      }
    })
  },

  /**
   * 获取电影s资讯
   */
  getTvsData: function(count) {
    var that = this;
    wx.request({
      url: api_url2,
      data: {
        count: count
      },
      success: (res) => {
        var subjects = res.data.subject_collection_items;

        subjects.forEach(function(item, index) {
          item.rate = null;
          try {
            item.rate = item.rating.value;
          } catch (error) {
            item.rate = 0;
          }
          var rate = item.rate;
          var heightLight = parseInt(parseInt(rate) / 2);
          var half = parseInt(rate) % 2;
          var gray = 5 - heightLight - half;
          var heightLights = [];
          var halfs = [];
          var grays = [];

          for (var i = 0; i < heightLight; i++) {
            heightLights.push(i);
          }

          for (var i = 0; i < half; i++) {
            halfs.push(i);
          }

          for (var i = 0; i < gray; i++) {
            grays.push(i);
          }

          item.heightLights = heightLights;
          item.halfs = halfs;
          item.grays = grays;
        })
        that.setData({
          modelData1: subjects
        })
      }
    })
  },
  /**
   * 获取电影资讯
   */
  getMovieData: function(count) {
    var that = this;
    wx.request({
      url: api_url1,
      data: {
        count: count
      },
      success: (res) => {
        var subjects = res.data.subject_collection_items;

        subjects.forEach(function(item, index) {
          item.rate = null;
          try {
            item.rate = item.rating.value;
          } catch (error) {
            item.rate = 0;
          }
          var rate = item.rate;
          var heightLight = parseInt(parseInt(rate) / 2);
          var half = parseInt(rate) % 2;
          var gray = 5 - heightLight - half;
          var heightLights = [];
          var halfs = [];
          var grays = [];

          for (var i = 0; i < heightLight; i++) {
            heightLights.push(i);
          }

          for (var i = 0; i < half; i++) {
            halfs.push(i);
          }

          for (var i = 0; i < gray; i++) {
            grays.push(i);
          }

          item.heightLights = heightLights;
          item.halfs = halfs;
          item.grays = grays;
        })
        that.setData({
          modelData3: subjects
        })
      }
    })
  }
})